Skip to content

ref(cloudflare): Use the shared request handler wrapper - #25588

Merged
isaacs merged 5 commits into
developfrom
isaacschlueter/js-3975-share-one-request-handler-wrapper-across-denoserve-bunserve-c
Oct 30, 2026
Merged

isaacs merged 5 commits into
developfrom
isaacschlueter/js-3975-share-one-request-handler-wrapper-across-denoserve-bunserve-c

Conversation

@isaacs

@isaacs isaacs commented Oct 20, 2026 •

Copy link
Copy Markdown
Member

Moves Cloudflare's wrapRequestHandler onto wrapServerRequestHandler, which Deno.serve and Bun.serve use since the previous PRs. Cloudflare keeps only what is unique to it: client init, the cf and cloud resource contexts, the protocol from cf.httpProtocol, the isStreamingResponse option, captureErrors, and the flush through waitUntil, which skips the dispose for a 101 response. The helpers that only the old code used (captureIncomingRequestBody, shouldCreateIncomingRequestSpan, getIncomingRequestSpanHooks and addRequest) are gone.

wrapRequestHandler must run on workers without the nodejs_compat flag, such as Shopify Oxygen, and every @sentry/server-utils entry loads node:async_hooks. So the wrapper moves to a new entry, @sentry/server-utils/request-handler, with no Node.js imports. Deno and Bun import it from there too. Cloudflare's module graph test allows this one entry, and a new test in server-utils walks the entry's module graph and fails on any Node.js import.

The wrapper gets three optional fields: isStreamingResponse, onDone (called once when the request is done, with the response if there is one), and a mechanism that can be left out to not capture errors.

For streamed responses, the shared stream monitor replaces Cloudflare's TransformStream. Workers with a compatibility date before 2022-11-30 cannot construct a ReadableStream without the streams_enable_constructors flag, so the wrapper then ends at handler return, as the TransformStream fallback did. The monitor now locks the source body only in start(), so the original response stays usable in that case. The wrapper builds the new response from the original one, which keeps encodeBody: 'manual'.

Visible changes for Cloudflare:

  • Request spans set client.address from CF-Connecting-IP (or another forwarding header), as on the other runtimes. This follows dataCollection.userInfo, which is true by default.
  • Request spans get the response header attributes, and events get the response context.
  • An error from the source of a streamed response is captured (unless captureErrors is false), and the span gets an error status.
  • If the user removed httpServerIntegration, request bodies follow dataCollection.httpBodies. Before, the SDK attached no body.

The minified withSentry bundle grows by about 2.4 KB (212,385 to 214,853 bytes), under the 211 KiB limit. Most of it is the request session and client address code, which Cloudflare now shares.

Fixes #25577
Fixes JS-3975

@linear-code

linear-code Bot commented Oct 20, 2026

Copy link
Copy Markdown

JS-3975

@isaacs
isaacs added this pull request to stack #25589 October 20, 2026 22:30

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stale Bugbot comment from a previous run.

@github-actions

github-actions Bot commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

size-limit report 📦

Compared: f032e07 (PR test merge using develop at b63640b)

Against: b63640b (develop) · base run #64512

✅ Baseline matches: this diff contains only this PR's changes.

Path Size % Change Change
@sentry/browser 30.39 kB - -
@sentry/browser - with treeshaking flags 28.54 kB - -
@sentry/browser - with treeshaking flags tracing without tracing 28.42 kB - -
@sentry/browser (incl. Tracing) 52.71 kB - -
@sentry/browser (incl. Tracing + Span Streaming) 52.72 kB - -
@sentry/browser (incl. Tracing, Profiling) 55.69 kB - -
@sentry/browser (incl. Tracing, Replay) 93.01 kB - -
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 81.61 kB - -
@sentry/browser (incl. Tracing, Replay with Canvas) 97.76 kB - -
@sentry/browser (incl. Tracing, Replay, Feedback) 110.75 kB - -
@sentry/browser (incl. Feedback) 47.98 kB - -
@sentry/browser (incl. sendFeedback) 35.5 kB - -
@sentry/browser (incl. FeedbackAsync) 40.6 kB - -
@sentry/browser (incl. Metrics) 31.43 kB - -
@sentry/browser (incl. Logs) 31.72 kB - -
@sentry/browser (incl. Metrics & Logs) 32.44 kB - -
@sentry/react 32.28 kB - -
@sentry/react (incl. Tracing) 55.08 kB - -
@sentry/vue 38.66 kB - -
@sentry/vue (incl. Tracing) 55.73 kB - -
@sentry/svelte 30.42 kB - -
@sentry/remix (Remix 3 client bundle) 57.79 kB - -
CDN Bundle 32.18 kB - -
CDN Bundle (incl. Tracing) 53.23 kB - -
CDN Bundle (incl. Logs, Metrics) 34.46 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) 55.17 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) 75.87 kB - -
CDN Bundle (incl. Tracing, Replay) 91.45 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) 93.38 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) 97.55 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) 99.55 kB - -
CDN Bundle - uncompressed 94.64 kB - -
CDN Bundle (incl. Tracing) - uncompressed 157.6 kB - -
CDN Bundle (incl. Logs, Metrics) - uncompressed 101.11 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) - uncompressed 163.34 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) - uncompressed 232.68 kB - -
CDN Bundle (incl. Tracing, Replay) - uncompressed 279.29 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) - uncompressed 285.01 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 293 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) - uncompressed 298.71 kB - -
@sentry/nextjs (client) 58.03 kB - -
@sentry/sveltekit (client) 53.23 kB - -
@sentry/core/server 44.24 kB - -
@sentry/core/browser 13.93 kB - -
@sentry/node 155.5 kB +0.02% +19 B 🔺
@sentry/node/import (ESM hook with diagnostics-channel injection) 82.16 kB - -
@sentry/node - without tracing 96.79 kB +0.03% +20 B 🔺
@sentry/node - without channel injection 135.86 kB +0.02% +20 B 🔺
@sentry/aws-serverless 104.99 kB +0.03% +21 B 🔺
@sentry/cloudflare (withSentry) - minified 200.64 kB +1.37% +2.71 kB 🔺
@sentry/cloudflare (withSentry) 499 kB +1.43% +7.03 kB 🔺
@sentry/nextjs/cloudflare (withSentry) - minified 221.18 kB +1.24% +2.7 kB 🔺

View base workflow run

@isaacs
isaacs force-pushed the isaacschlueter/js-3975-share-one-request-handler-wrapper-across-denoserve-bunserve-c branch from fc30522 to 5d3ee29 Compare October 21, 2026 01:06
@isaacs
isaacs marked this pull request as ready for review October 21, 2026 01:07
@isaacs
isaacs requested a review from a team as a code owner October 21, 2026 01:07
@isaacs
isaacs requested review from JPeer264 and andreiborza and removed request for a team October 21, 2026 01:07
@isaacs
isaacs force-pushed the isaacschlueter/js-3975-share-one-request-handler-wrapper-across-denoserve-bunserve-c branch from 5d3ee29 to 7a1874d Compare October 21, 2026 01:07
@isaacs
isaacs requested a review from a team as a code owner October 21, 2026 01:13
@isaacs
isaacs requested review from chargome and nicohrubec and removed request for a team October 21, 2026 01:13
@isaacs
isaacs force-pushed the isaacschlueter/js-3975-share-one-request-handler-wrapper-across-denoserve-bunserve-c branch from 7a1874d to 9e86e46 Compare October 21, 2026 01:13
@isaacs
isaacs force-pushed the isaacschlueter/js-3975-share-one-request-handler-wrapper-across-denoserve-bunserve-c branch 2 times, most recently from a650894 to 66b9312 Compare October 21, 2026 01:20

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stale Bugbot comment from a previous run.

@isaacs
isaacs force-pushed the isaacschlueter/js-3975-share-one-request-handler-wrapper-across-denoserve-bunserve-c branch from a1a478b to f0da1bc Compare October 21, 2026 04:14
@isaacs
isaacs force-pushed the isaacschlueter/js-3975-share-one-request-handler-wrapper-across-denoserve-bunserve-c branch from f0da1bc to 128c6ee Compare October 21, 2026 17:15

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stale Bugbot comment from a previous run.

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 5e42003. Configure here.

@isaacs
isaacs force-pushed the isaacschlueter/js-3975-share-one-request-handler-wrapper-across-denoserve-bunserve-c branch 2 times, most recently from 3c42ed4 to 0ec0e9c Compare October 26, 2026 19:35
Base automatically changed from isaacschlueter/js-3975-share-one-request-handler-wrapper-across-denoserve-bunserve-b to develop October 26, 2026 23:27
@isaacs
isaacs force-pushed the isaacschlueter/js-3975-share-one-request-handler-wrapper-across-denoserve-bunserve-c branch from 0ec0e9c to 06363f7 Compare October 26, 2026 23:27
Comment thread CHANGELOG.md Resolved
Comment thread .size-limit.js Resolved
@isaacs
isaacs force-pushed the isaacschlueter/js-3975-share-one-request-handler-wrapper-across-denoserve-bunserve-c branch from 06363f7 to 2be491f Compare October 29, 2026 17:09
@isaacs
isaacs requested a review from a team as a code owner October 29, 2026 17:09
@isaacs
isaacs requested review from chargome, logaretm, msonnb and mydea and removed request for a team October 29, 2026 17:09
@isaacs
isaacs force-pushed the isaacschlueter/js-3975-share-one-request-handler-wrapper-across-denoserve-bunserve-c branch 2 times, most recently from 05dc8e3 to 881f9f5 Compare October 29, 2026 17:39

@logaretm logaretm left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, but a minor thing that got flagged when I was testing this for the tunnels work.

isaacs and others added 5 commits October 30, 2026 11:41
Moves Cloudflare's `wrapRequestHandler` onto `wrapServerRequestHandler`,
which `Deno.serve` and `Bun.serve` use since the previous PRs. Cloudflare
keeps only what is unique to it: client init, the `cf` and cloud
resource contexts, the protocol from `cf.httpProtocol`, the
`isStreamingResponse` option, `captureErrors`, and the flush through
`waitUntil`, which skips the dispose for a 101 response. The helpers
that only the old code used (`captureIncomingRequestBody`,
`shouldCreateIncomingRequestSpan`, `getIncomingRequestSpanHooks` and
`addRequest`) are gone.

`wrapRequestHandler` must run on workers without the `nodejs_compat`
flag, such as Shopify Oxygen, and every `@sentry/server-utils` entry
loads `node:async_hooks`. So the wrapper moves to a new entry,
`@sentry/server-utils/request-handler`, with no Node.js imports. Deno
and Bun import it from there too. Cloudflare's module graph test allows
this one entry, and a new test in server-utils walks the entry's module
graph and fails on any Node.js import.

The wrapper gets three optional fields: `isStreamingResponse`, `onDone`
(called once when the request is done, with the response if there is
one), and a `mechanism` that can be left out to not capture errors.

For streamed responses, the shared stream monitor replaces Cloudflare's
`TransformStream`. Workers with a compatibility date before 2022-11-30
cannot construct a `ReadableStream` without the
`streams_enable_constructors` flag, so the wrapper then ends at handler
return, as the `TransformStream` fallback did. The monitor now locks the
source body only in `start()`, so the original response stays usable in
that case. The wrapper builds the new response from the original one,
which keeps `encodeBody: 'manual'`.

Visible changes for Cloudflare:

- Request spans set `client.address` from `CF-Connecting-IP` (or
  another forwarding header), as on the other runtimes. This follows
  `dataCollection.userInfo`, which is `true` by default.
- Request spans get the response header attributes, and events get the
  `response` context.
- An error from the source of a streamed response is captured (unless
  `captureErrors` is `false`), and the span gets an error status.
- If the user removed `httpServerIntegration`, request bodies follow
  `dataCollection.httpBodies`. Before, the SDK attached no body.

The minified `withSentry` bundle grows by about 2.4 KB (212,385 to
214,853 bytes), under the 211 KiB limit. Most of it is the request
session and client address code, which Cloudflare now shares.

Fixes #25577
Fixes JS-3975

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
…ncLocalStorage

Without `AsyncLocalStorage` (for example on Shopify Oxygen, or on
Workers without `nodejs_compat`), the SDK uses the stack fallback, and
`withInvocationIsolationScope` does not fork. The scope it passes to its
callback is a clone that is not the current isolation scope. The shared
request handler wrote the request data and body to that clone, so an
event captured in the handler, which reads the current scope, had no
request body. The `hydrogen-react-router-7` E2E test caught this.

The handler now gets the current isolation scope. With
`AsyncLocalStorage`, that is the same scope as before. Without it, the
request data goes where the body went before the move to the shared
handler, and events now also get the request URL and method.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
…text

Without `AsyncLocalStorage`, all requests use the default isolation
scope, which lives as long as the isolate. `setSDKProcessingMetadata`
merges into the existing `normalizedRequest`, so a request without a
body kept the body of an earlier request. The `response` context of
the earlier request also stayed set. For example, an error in a `GET`
after a `POST /login` got the login password as `request.data` and the
status code of the `POST` response.

The shared request handler now clears both at the start of each
request.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@isaacs
isaacs force-pushed the isaacschlueter/js-3975-share-one-request-handler-wrapper-across-denoserve-bunserve-c branch from 881f9f5 to 7a0c28f Compare October 30, 2026 18:42
@isaacs
isaacs requested a review from logaretm October 30, 2026 18:58

@logaretm logaretm left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

@isaacs
isaacs enabled stacked auto-merge (squash) October 30, 2026 19:02
@isaacs
isaacs merged commit c16633e into develop Oct 30, 2026
539 of 570 checks passed
@isaacs
isaacs deleted the isaacschlueter/js-3975-share-one-request-handler-wrapper-across-denoserve-bunserve-c branch October 30, 2026 19:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Share one  Request  handler wrapper across Deno.serve, Bun.serve and Cloudflare

4 participants